"""
3网易云断点参数生成的问题
参数以第一次断到的参数 为准      实在不行 在往下执行

4切换到之前断点的位置
 把所有的断点清空 点击最上面的 蓝色按钮 回到正常页面     再把断点打上  再点击下一页（执行会断点的操作）


爬取 任意歌曲 5页网易云评论
保存json格式文件

猜想 pageNo=4就是第四页的数据 pageNo 控制着页数的数据呢
结合实际操作 是能行的

其中包含重复数据
"""



import execjs
# 1读取js文件
import requests
import json
import re
contentslist=[]
num=5
d = {"rid":"R_SO_4_1826307499",
     "threadId":"R_SO_4_1826307499",
     "pageNo":num,
     "pageSize":num*20,
     "cursor":"1650562275327",
     "offset":"0",
     "orderType":"1",
     "csrf_token":""}
da=json.dumps(d)

with open("网易云.js","r",encoding="utf-8")as file1:
    jsdata=file1.read()

# 2通过将读取出来的 js代码 传递给 execjs的compile方法  返回一个 执行器对象
exe=execjs.compile(jsdata)
# 3通过执行器对象执行 js代码里面的call函数   call方法的第二个参数 是传递给 方法的参数
result=exe.call("get",da)
# 4得到函数抛出的返回值
print(result)

# 评论
contenturl="https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
"""
"rid":"R_SO_4_1826307499 
1826307499  是每首歌的歌曲id  
"""
data={
    "params":result["encText"],
    "encSecKey":result["encSecKey"]
}

headers={
    "cookie": "NTES_P_UTID=Yklzz9TS3OquVzILtYxhaNDpNc5YrLBC|1646749579; P_INFO=jiuhaoyyds@163.com|1646749579|0|mail163|00&99|null&null&null#sic&510100#10#0#0|&0|mail163|jiuhaoyyds@163.com; nts_mail_user=jiuhaoyyds@163.com:-1:1; _ntes_nnid=eb55f8bc260dc3ecc0eb8f07db5b4aa5,1646749584744; _ntes_nuid=eb55f8bc260dc3ecc0eb8f07db5b4aa5; NMTID=00OAs-1hi1-5wcgFU3HlA2gKhqUwx8AAAF_aeyKoQ; WNMCID=iyzbho.1646749584881.01.0; WEVNSM=1.0.0; WM_TID=nNtS2GTdkAlFBBBQBQZv7ynH%2FfCQm%2F3T; _iuqxldmzr_=32; WM_NI=L1WirorGlM3qC8emwZDrCpUM%2BSOt1nSSC%2FdntNYsCVrQwovMKHEoGLF9yZSySWtlgio8MG9%2BynX3Cy%2BW1v9EDdoomeBnAs6LUoVCrebgnDvxIhB77mtWIrp%2BDg1cLNgHUHE%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee84f24bb5b6bab4cc609c9a8aa2d14f829b8b87d14f98869bd7d970a3b88197b62af0fea7c3b92a90ef998af165b299a4aeb27b81e8ab8ef65288ae8d91d847e98d8195b466918d8dd0e766b8929794c25eb28fb990f54789988b90c64886e7b7bbd2739189bc8de663ade883a8bc21bbb298d4c273e989ff97bc4f88bca190f06f82b28f94b54897b19aa4f16fa2b9a2a5f5748bb88ed8b667b8898793f32188b9ff84cf46a5989ba9c837e2a3; JSESSIONID-WYYY=c8256qOYjFrxpSGhXPrAV8sZ3s1dIAVHsl0kjy3H%5CkzPKx732hI9XI1fsWiWTi%2BoixQu8G6YViCV%5C%5Ce6hM0jui1lRKiPh1PeRc935bTPNCs9MAxbxYyRXFYRnv6%5C7%2BloAEoC99snZYw28sKloWnc49JUq%2F2FpI3k8xWpQMrbkypev8%5CR%3A1650630185037",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
    "referer": "https://music.163.com/song?id=1826307499"
}
response=requests.post(url=contenturl,headers=headers,data=data)
data=response.content.decode()
print(data)
contenet=re.findall('"content":"(.*?)","status',data)
for i in contenet:
    if i not in contentslist:
        contentslist.append(i)
# 数据清洗
# print(contenet)
print(len(contentslist))

with open("content.json","w",encoding="utf-8")as f:
    strdata=str(contentslist)
    f.write(strdata)




